package cn.edu.scau.cmi.liangzaoqing.fxml;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.sql.SQLException;
import java.util.Map;
import java.util.ResourceBundle;

import javax.xml.bind.JAXBException;

import org.apache.metamodel.schema.Schema;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.TextField;
import javafx.scene.layout.Pane;
import javafx.stage.DirectoryChooser;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
import javafx.stage.FileChooser.ExtensionFilter;

public class SchemaInfoController implements Initializable {
	private Schema[] schemas;

	private Map<String, String> databaseInfo;

	@FXML
	private Button fileBrowserButton;

	@FXML
	private Button xmlBrowserButton;

	@FXML
	private Button nextButton;

	@FXML
	private ChoiceBox<String> schemaChoiceBox;

	@FXML
	private TextField xmlBrowserTextField;

	@FXML
	private TextField fileBrowserTextField;

	@Override
	public void initialize(URL location, ResourceBundle resources) {

	}

	public void nextStep() throws IOException, JAXBException {
		//已经点击浏览物理主键文件按钮
		if(databaseInfo.containsKey("physicalPKXmlFilePath")) {
		}else {//没有点击浏览物理主键文件按钮
			databaseInfo.put("physicalPKXmlFilePath", "");
		}
		Stage nextStage = new Stage();
		nextStage.setTitle("数据库物理模型创建应用");
		FXMLLoader loader = new FXMLLoader(getClass().getResource("PhysicalPrimaryKeySelectPage.fxml"));
		Pane nextPane = loader.load();

		PhysicalPrimaryKeySelectController physicalPrimaryKeySelectController = (PhysicalPrimaryKeySelectController) loader
				.getController();

		physicalPrimaryKeySelectController.setSchema(getChosenSchema(schemaChoiceBox.getValue(), schemas));
		physicalPrimaryKeySelectController.setDatabaseInfo(databaseInfo);
		physicalPrimaryKeySelectController.initTreeView();
		Scene nextScene = new Scene(nextPane);
		nextStage.setScene(nextScene);
		nextStage.setResizable(false);
		nextStage.show();
		Stage stage = (Stage) nextButton.getScene().getWindow();
		stage.close();
	}

	public void initializeSchema(Schema[] schemas) throws ClassNotFoundException, SQLException {
		this.schemas = schemas;
		ObservableList<String> schemaOptions = FXCollections.observableArrayList();

		for (Schema schema : schemas) {
			schemaOptions.add(schema.getName());
		}
		schemaChoiceBox.setItems(schemaOptions); // this statement adds all values in choiceBox
		schemaChoiceBox.setValue("ema");
	}

	public void setDatabaseInfo(Map<String, String> databaseInfo) {
		this.databaseInfo = databaseInfo;
	}

	public void browserFile() {
		FileChooser fileChooser = new FileChooser();
		fileChooser.setTitle("请选择物理主键XML文件");
		String path = System.getProperty("user.dir") + "\\model";
		File defaultDirectory = new File(path);
		fileChooser.setInitialDirectory(defaultDirectory);
		fileChooser.getExtensionFilters().add(new ExtensionFilter("Text Files", "*.xml"));
		File file = fileChooser.showOpenDialog(fileBrowserButton.getScene().getWindow());
		//已经选择物理主键文件
		if(file.exists()) {
			fileBrowserTextField.setText(file.getAbsolutePath());
			databaseInfo.put("physicalPKXmlFilePath", file.getAbsolutePath());
		}else {//虽然点击选择物理主键按钮，但是没有选择物理主键文件，即空
			databaseInfo.put("physicalPKXmlFilePath","");
		}
		
		

	}

	public void browserXmlLocation() {
		DirectoryChooser directoryChooser = new DirectoryChooser();
		directoryChooser.setTitle("请选择XML文件的存放目录");
		String path = System.getProperty("user.dir");
		File defaultDirectory = new File(path);
		directoryChooser.setInitialDirectory(defaultDirectory);
		File directory = directoryChooser.showDialog(xmlBrowserButton.getScene().getWindow());
		xmlBrowserTextField.setText(directory.getAbsolutePath());
		databaseInfo.put("genarateXmlFileDirectoryPath", directory.getAbsolutePath());
	}

	public Schema getChosenSchema(String schemaName, Schema[] schemas) {
		System.out.println("进来比较的schemaName" + "[" + schemaName + "]");
		for (Schema scm : schemas) {
			if (scm.getName().equalsIgnoreCase(schemaName)) {
				return scm;
			}
		}
		return null;
	}
}
